<template>
    <div class="sear-box">
        <van-nav-bar
            right-text="搜索"
            left-arrow
            @click-left="router.back()"
            @click-right="seaEvt"
        >
            <template #title>
                <van-search 
                    v-model="search" 
                    @search="seaEvt" 
                    @click-left-icon="seaEvt" 
                    @focus="show = true" 
                    placeholder="请输入搜索关键词" 
                />
            </template>
        </van-nav-bar>
        <main>
            <div class="recommend" v-show="show">
                <h5>热门推荐</h5>
                <div class="hot">
                    <span v-for="it in recommend" :key="it" @click="recoEvt(it)">{{ it }}</span>
                </div>
            </div>
            <h5>猜你喜欢</h5>
            <div class="like" :class="[show == true && 'act']">
                <div class="goods" v-for="it in goodsList" :key="it.id" @click="detEvt(it.id)">
                    <img :src="it.img">
                    <span>{{ it.title }}</span>
                </div>
            </div>
        </main>
    </div>
</template>

<script lang="ts" setup>
import { ref, Ref } from 'vue';
import router from '../../router';
import { goodsSearchApi } from '../../apis/goodsApi';
import detEvt from '../details/detEvt';

const search:Ref = ref('')
search.value = sessionStorage.getItem('search') || ''
const recommend:any = ref(['女装', '包包', '鞋子', '泳装', '手表'])
const show:Ref<boolean> = ref(false)

if(search.value) {
    searchEvt(search.value)
} else {
    searchEvt('女装')
}

/** 热门推荐 */
function recoEvt(des: string) {
    location.reload()
    searchEvt(des)
    show.value = false
    search.value = des
    sessionStorage.setItem('search', des)
}

/** 搜索 */
function seaEvt() {
    location.reload()
    searchEvt(search.value)
    sessionStorage.setItem('search', search.value)
}

/** 商品列表 */
const goodsList:any = ref([])
async function searchEvt(val: string) {
    let res = await goodsSearchApi(val)
    goodsList.value = res.data
}
</script>

<style lang="less" scoped>
@import '../../style/common.less';

.sear-box {
    width: 100vw;
    height: 100vh;
    padding-top: 10px;

    .van-search {
        width: 300px;
    }

    main {
        width: 100%;
        height: 100%;

        h5 {
            margin:10px 0;
            text-indent: 5px;
            border-left: 4px solid @g5;
        }

        .recommend {
            width: 100%;

            .hot {
                display: flex;
                flex-wrap: wrap;

                span {
                    font-size: 12px;
                    background-color: @b5;
                    padding: 8px 12px;
                    border-radius: 20px;
                    margin: 10px;
                }
            }
        }   

        .like {
            width: 100%;
            height: calc(100% - 88px);
            padding: 0 10px;
            display: flex;
            flex-wrap: wrap;
            overflow-x: hidden;
            &::-webkit-scrollbar {
                display: none;
            }
            
            .goods {
                width: calc(100% / 2);
                padding: 10px;

                img {
                    width: 100%;
                    height: 165px;
                }

                span {
                    font-size: 12px;
                    text-align: left;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                    overflow: hidden;
                }
            }
        }
    }
}

.act {
    height: calc(100% - 182px) !important;
}

::v-deep .van-nav-bar .van-icon {
    color: @g5;
    font-size: 20px;
}
::v-deep .van-nav-bar__text {
    font-size: 14px;
    font-weight: bolder;
    color: @g5;
}
::v-deep .van-nav-bar__title {
    max-width:none;
}
::v-deep .van-search__field {
    font-weight: normal;
}
::v-deep .van-icon-search {
    color: @c3 !important;
}
</style>
